package com.yxw.live_vod_backend_boot;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yxw.yxnet_cd_center.common.converter.MultipartJackson2HttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.env.Environment;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.annotation.PostConstruct;
import java.io.File;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;
import java.util.TimeZone;

@Slf4j
@SpringBootApplication(exclude = { MongoDataAutoConfiguration.class})
@MapperScan("com.yxw.live_vod_backend_boot.mapper")//mapper接口扫描注解
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {
        "com.yxw.live_vod_facade.feign"
})
@EnableSwagger2
@EnableScheduling
@EnableAsync
@EnableAspectJAutoProxy
public class LiveVodBackendBootApplication implements WebMvcConfigurer {

    private static String AppCurrenDir = "";
    public static String getAppCurrenDir() {
        return AppCurrenDir;
    }

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * 设置系统默认时区"Asia/Shanghai"
     */
    @PostConstruct
    public void init() {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai")); // 设置默认时区
        log.info("默认时区为: ====================" + TimeZone.getDefault().getID());
    }

    public static void main(String[] args) {
        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
        ConfigurableApplicationContext application = SpringApplication.run(LiveVodBackendBootApplication.class, args);
        Environment env = application.getEnvironment();
        String ip;
        try
        {
            ip = InetAddress.getLocalHost().getHostAddress();
        }
        catch(UnknownHostException e)
        {
            ip = "";
        }
        String port = env.getProperty("server.port");
        String contextPath = env.getProperty("server.servlet.context-path");
        contextPath = null==contextPath?"":contextPath;
        File oFile = new File(com.yxw.live_vod_backend_boot.LiveVodBackendBootApplication.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        AppCurrenDir = oFile.getParent().replaceAll("file:\\\\", "").replaceAll("\\\\live-vod-backend-boot-1.0.0-SNAPSHOT.jar!\\\\BOOT-INF", "");
        log.info("--------------------------------------------------------------------------------------------------------------------\n\t" +
                "直播管理后台服务 is running! Access URLs:\n\t" +
                "Local: \t\thttp://localhost:" + port + contextPath + "/\n\t" +
                "Swagger文档: \thttp://" + ip + ":" + port + contextPath + "/doc.html\n\t" +
                "AppCurrenDir:" + AppCurrenDir + "\n\t" +
                "==========================================直播管理后台服务启动完成==========================================");
    }

    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MultipartJackson2HttpMessageConverter(objectMapper));
    }

}
